import { defineStore } from 'pinia'

const USERNAME_DEFAULT = '';

const options = {
  state: () => {
    return { 
      username: USERNAME_DEFAULT
     }
  },
  getters: {
    //获取登录状态
    logined(state) {
      return Boolean(state.username);
    },
    //获取已登录用户名
    loginName(state) {
      return state.username;
    }
  },
  actions: {
    //登录
    signIn(username) {
      //将已登录的用户的用户名保存起来
      this.username = username;
    },
    //登出
    signOut() {
      //将username修改为默认值
      this.username = USERNAME_DEFAULT;
    }
  }
}

//在定义变量或常量时可用export直接将其导出（属于按需导出）
export const useLoginStore = defineStore('login', options)
